<?php

namespace Yi\Permission\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Yi\Permission\Models\Auth\Role;
use Yi\Permission\Models\User;
use App\Models\Community;

class UserController extends Controller
{
    public $request;
    public $model;

    public function __construct(Request $request, User $model)
    {
        $this->request = $request;
        $this->model = $model;
    }
    public function index()
    {
        $pageSize = (int) request()->input('pageSize', 10);
        $data = $this->model->orderBy($this->defaultSorts, $this->defaultOrder)->paginate($pageSize, ['*'], 'current');
        $data->transform(function ($value) {
            $value->roles_list = Role::whereIn('id', (array) $value->role_ids)->select(['id', 'name'])->get();
            $value->community_list = Community::whereIn('id', (array) $value->community_ids)->select(['id', 'name'])->get();
            return $value;
        });
        return success($data);
    }
}
